package com.innovation.chengzhangriji.ui;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.Html5;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;

public class MmsFragment extends BaseFragment {

//    private XWalkView webView;
    private WebView webView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(contentView == null) {
            contentView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_mms, null);
        }
        return contentView;
    }

    private void setLayout() {
        // webView
//        webView = (XWalkView) contentView.findViewById(R.id.webView);
//        webView.clearCache(true);
//        webView.load(Html5.MAMISHUO, null);

        // 为兼容4.4之前的webView这种方式可防止内存泄漏
        webView = new WebView(getContext().getApplicationContext());
        webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        webView.setBackgroundColor(getContext().getResources().getColor(R.color.theme_bg));
        if(Client.isNetworkConnect(getContext())) {
            webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        }
        ((LinearLayout)contentView).addView(webView);
    }

    @Override
    public void onResume() {
        super.onResume();
        setLayout();
        webView.loadUrl(Html5.MAMISHUO + "?uid="+ Config.getLoginInfo(getContext()).getUserID());
        // System.out.println("加载了妈妈说");
    }

    @Override
    public void onStop() {
        if(webView != null) {
            webView.removeAllViews();
            ((LinearLayout)contentView).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onStop();
        // System.out.println("结束了妈妈说");
    }
}
